<?php
date_default_timezone_set("PRC");
header('Content-Type:text/html;charset=utf-8');
function pub($config){
$link=@mysql_connect($config['host'],$config['user'],$config['pwd']);
if(!$link){
    die('数据库连接错误!'.mysql_error().',错误号是'.mysql_errno());
}
mysql_select_db($config['bd']);
mysql_set_charset($config['char']);    
}

function selects($config,$table,$where){
pub($config);
$sql="select * from {$table} {$where} order by id";
$res=mysql_query($sql);
$arr=array();
while($row=mysql_fetch_assoc($res)){
    $arr[]=$row;
}
//var_dump($sql);die;
mysql_free_result($res);
mysql_close();
return $arr;
}   

function add($config,$table,$post=''){
pub($config); 
if($post==''){
    $post=$_POST;
}
$fields='`'.implode('`,`',array_keys($post)).'`';
$values="'".implode("','",$post)."'";
$sql="insert into $table ($fields) values ($values)";
$rec=mysql_query($sql);
//var_dump($sql);
//var_dump($rec);die;
mysql_close($rec);
return $rec; 
}

function del($config,$table,$id){
pub($config);
$sql="delete from $table where id={$id}";
$rec=mysql_query($sql);
mysql_close($rec);
return $rec;    
}

function xiugai($config,$table,$where,$post=''){
pub($config);
if($post==''){
    $post=$_POST;
}
$chage='';
foreach ($post as $k => $v) {
    $chage.="`{$k}`='{$v}',";
}
$chage=rtrim($chage,',');
$sql="update {$table} set {$chage} {$where}";
$rec=mysql_query($sql);
mysql_close($rec);
return $rec;
}

function select($config,$table,$where){
$p=isset($_GET['p'])?$_GET['p']:1;
pub($config);
$offset=($p-1)*$config['everypage'];
$sql="select * from {$table} {$where} order by id limit {$offset},{$config['everypage']}";
$sqlcount="select count(*) as c from {$table}";

$rescount=mysql_query($sqlcount);
$count=mysql_fetch_assoc($rescount)['c'];
$countpage=ceil($count/$config['everypage']);
$prev=$p-1;
if($prev=='0'){
   $prev=1;
}
$next=$p+1;
if($next>$countpage){
  $next=$countpage;
}
$first=1;
$_SESSION['first']=$first;
$_SESSION['prev']=$prev;
$_SESSION['next']=$next;
$_SESSION['countpage']=$countpage;
$_SESSION['p']=$p;
//var_dump($countpage);
$res=mysql_query($sql);
$arr=array();
while($row=mysql_fetch_assoc($res)){
    $arr[]=$row;
}
mysql_free_result($rescount);
mysql_free_result($res);
//var_dump($sql);
mysql_close();
return $arr;
}

function timeselect($config,$table,$where){
$p=isset($_GET['p'])?$_GET['p']:1;
pub($config);
$offset=($p-1)*$config['everypage'];
$sql="select * from {$table} {$where} order by ctime limit {$offset},{$config['everypage']}";
$sqlcount="select count(*) as c from {$table}";

$rescount=mysql_query($sqlcount);
$count=mysql_fetch_assoc($rescount)['c'];
$countpage=ceil($count/$config['everypage']);
$prev=$p-1;
if($prev=='0'){
   $prev=1;
}
$next=$p+1;
if($next>$countpage){
  $next=$countpage;
}
$first=1;
$_SESSION['first']=$first;
$_SESSION['prev']=$prev;
$_SESSION['next']=$next;
$_SESSION['countpage']=$countpage;
$_SESSION['p']=$p;
//var_dump($countpage);
$res=mysql_query($sql);
$arr=array();
while($row=mysql_fetch_assoc($res)){
    $arr[]=$row;
}
mysql_free_result($rescount);
mysql_free_result($res);
//var_dump($sql);
mysql_close();
return $arr;
}

function up($name, $dir = '/admin/zuopin/wenjian', $max_size =2000000, $allow_type = array('jpg', 'png', 'gif'))
{
    $file = $_FILES[$name];
    
    // var_dump($file);
    $ext  = trim(strchr($file['name'], '.'), '.');
    // var_dump($ext);die;
    //判断错误号
    if ($file['error'] != 0) {
        echo  "<script>alert('上传错误');window.location.href='/home/landing/registered/registered.php'</script>";
        switch ($_FILES['myfile']['error']) {
            case 1:die('上传文件大小超出了PHP配置中的约定值：upload_max_filesize');
            case 2:die('上传文件大小超出了表单中的约定值：MAX_FILE_SIZE');
            case 3:die('文件只被部分上载');
            case 4:die('没有上传任何文件');
            case 6:die('找不到临时文件夹');
            case 7:die('文件写入失败');
            default:die('末知错误');
        }
    }

    //判断上传的文件大小
    if ($file['size'] > $max_size) {
        die('上传文件太大了我受不了！');
    }

    //var_dump($ext,$allow_type);
    //判断上传的文件格式
    if (!in_array($ext, $allow_type)) {
        die('你当我瞎啊，请传正确的格式！');
    }

    //随机文件名
    $filename = date('YmdHis') . mt_rand(100, 999) . '.' . $ext;
    // echo $filename;die;
  // echo $file['tmp_name'];die;
    if (is_uploaded_file($file['tmp_name'])) {
        // if (!file_exists($dir)) {
        //     $res = mkdir($dir);
        // }
        if (move_uploaded_file($file['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/'.trim($dir, '/') . '/' . $filename)) {

            return  '/'.trim($dir, '/') . '/' . $filename;
        } else {
            echo '错误：'.$_SERVER['DOCUMENT_ROOT'].'/'.trim($dir, '/');
        }
    } else {
        die('非法上传');
    }
}

?>